const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

// 设置 nodejs 环境变量，默认是production
// process.env.NODE_ENV = "development"

module.exports = {
  mode: 'development',
  entry: './src/js/index.js',
  output: {
    filename: 'js/[name].bundle.js',
    path: resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          // 创建 style 标签，将样式放入
          // 'style-loader',
          // 这个 loader 取代 style-loader。作用：提取 css 到单独的文件中
          MiniCssExtractPlugin.loader,
          // 将 css 整合到 js 中
          'css-loader',
          /*
          * css 兼容性处理：postcss --> postcss-loader postcss-preset-env
          * postcss-preset-env：帮助 postcss 找到 package.json 中 browserslist 里面的配置，通过配置加载指定的 css 兼容性样式
          *
          *   "browserslist": {
          *     // 开发环境 --> 设置环境变量：process.env.NODE_ENV = development
                "development": [
                  "last 1 chrome version",
                  "last 1 firefox version",
                  "last 1 safari version"
                ],
                // 生产环境，默认是生产环境
                "production": [
                  ">0.2%",
                  "not dead",
                  "not op_mini all"
                ]
              }
          *
          * */
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins: [
                  "postcss-preset-env"
                ]
              }
            }
          }
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./src/index.html"
    }),
    new MiniCssExtractPlugin({
      filename: 'css/[hash:10].css'
    })
  ]

}